<?php
if (!defined( "IN_UCHOME" ) ) exit( "Access Denied" );
require('class.action.php');
class modify extends action
{
	var $op = '';
	var $id = 0;
	var $param = array();
	var $uid = 0;

	function modify($uid)
	{
		$this->uid = $uid;
	}

	function get_data($id)
	{
		if($id > 0){
			$temp = $this->query_one("select * from ".tname("app_action")." where id = '{$id}' ");
			if(empty($temp) || ($this->uid != $temp['uid'] && $this->uid != ADMIN_UID)) $this->error('你提交了非法的请求数据', 'action.php?ac=index', 0);
			return $temp;
		}
	}

	function get_request()
	{
		$this->id = intval($_GET['id']);
		$this->param['subject'] = $this->get_post('subject');
		if(empty($this->param['subject'])) $this->error('请输入活动的名称');
		$this->param['type'] = intval($_POST['type']);
		if($this->param['type'] == 1){
			$this->param['type_company'] = '';
		}else{
			$this->param['type_company'] = $this->get_post('type_company');
			if(empty($this->param['type_company'])) $this->error('请输入活动主办方的名称');
		}
		$this->param['p'] = $this->get_post('p');
		$this->param['c'] = $this->get_post('c');
		if(empty($this->param['p']) || empty($this->param['c'])) $this->error('请选择活动举办城市');
		$this->param['area'] = $this->get_post('area');
		if(empty($this->param['area'])) $this->error('请输入活动地点');
		$this->param['person_num'] = intval($_POST['person_num']);
		include_once(S_ROOT . "/source/function_blog.php");
		$_POST['description'] = checkhtml($_POST['description']);
		$_POST['description'] = getstr($_POST['description'], 0, 1, 0, 1, 0, 1);
		$_POST['description'] = preg_replace("/\<div\>\<\/div\>/i", '', $_POST['description']);	
		$this->param['description'] = $_POST['description'];
		if(empty($this->param['description'])) $this->error('请输入活动描述，让其他人知道活动的目的');
		$this->param['eventtype'] = $this->get_post('eventtype');
		if(empty($this->param['eventtype'])) $this->error('请选择活动分类');
		$this->param['price_type'] = $this->get_post('price_type');
		$this->param['price_description'] = $this->get_post('price_description');
		$this->param['collect_area'] = $this->get_post('collect_area');
		if(empty($this->param['collect_area'])) $this->error('请输入集合地点');
		$this->param['contact'] = $this->get_post('contact');
		if(empty($this->param['collect_area'])) $this->error('请输入发起人的联系方式');
		$this->param['collect_type'] = is_array($_POST['collect_type']) ? array_sum($_POST['collect_type']) : 0;
		$this->param['privite'] = intval($_POST['privite']);

		$p = $this->get_picture();
		if($p !== false){
			if($p['status'] != 200) $this->error($p['message']);
			$this->param['picture'] = $p['message'][0] ;
		}

		if($this->id > 0){
			$temp = $this->get_data($this->id);
			if(empty($temp)) $this->error('当前活动已经不存在了', 'action.php', 3);
			if($temp['uid'] != $this->uid && $this->uid != ADMIN_UID) $this->error('你没有权限修改活动', 'action.php', 3);
			return $this->update();
		}else{
			global $_SGLOBAL;
			$this->param['start_time'] = intval($this->get_time('start_time'));
			$this->param['end_time'] = intval($this->get_time('end_time'));
			if($this->param['end_time'] < $this->param['start_time'] || $this->param['end_time'] < $_SERVER['REQUEST_TIME'] ) $this->error('活动的起始时间有误');
			$this->param['join_num'] = 1;
			$this->param['uid'] = $this->uid;
			$this->param['username'] = addslashes($_SGLOBAL['supe_username']);
			$this->param['dateline'] = $_SERVER['REQUEST_TIME'];
			return $this->add();
		}
	}

	function error($message, $url = '', $time = 2, $type = 0)
	{
		global $_SC;
		$time = $time * 1000;
		header('Content-type: text/html; charset=' . $_SC['charset']);
		$title = $type == 0 ? '出错了' : '成功了';
		echo "<script type=\"text/javascript\">try{parent.base.alert('{$title}', '<p>{$message}</p>');}catch(e){alert('$message');};</script>";
		if($url) echo "<script type=\"text/javascript\">setTimeout(function(){top.location.href='{$url}';},{$time});</script>";
		exit();
	}

	function get_post($name)
	{
		return htmlspecialchars(preg_replace("/<.*?>/", '', trim($_POST[$name])));
	}

	function get_time($name)
	{
		$p = $_POST[$name];
		return mktime($p[3], $p[4], 0, $p[1], $p[2], $p[0]);
	}

	function get_picture()
	{
		return $this->upload();
	}

	function add()
	{
		$eventid = inserttable('app_action', $this->param, true);
		if($this->param['privite'] != 2) $this->add_feed($eventid);
		return $eventid;
	}

	function update()
	{
		updatetable('app_action', $this->param, array('id'=>$this->id));
	}

	function add_feed($eventid)
	{
		$fs = array();
		$fs['icon'] = 'event';
		$fs['title_template'] = "<b>{actor} 发布了一个活动</b>";
		$fs['body_template'] = '';
		$fs['body_general'] = "<br>主题：<a href='action.php?ac=show&id={$eventid}'>{$this->param['subject']}</a></b><br />类型：{$this->param['eventtype']}<br />地点：{$this->param['p']} {$this->param['c']} {$this->param['area']}";
		$fs['images'] = array($this->param['picture'] ? $this->param['picture'] : 'action/img/event.png');
		$fs['image_links'] = array("action.php?ac=show&id={$eventid}");
		$fs['body_data'] = array();

		include_once(S_ROOT.'./source/function_cp.php');
		$feedid = feed_add($fs['icon'], $fs['title_template'], $fs['title_data'], $fs['body_template'], $fs['body_data'], $fs['body_general'],$fs['images'], $fs['image_links'], $fs['target_ids'], $fs['friend'], UC_APPID, 1);

	}
}
?>